<!DOCTYPE html>
<html lang="en"  xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>仓库管理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="../../../../resources/lib/layui-v2.5.5/css/layui.css" media="all" />
    <link rel="stylesheet" href="../../../../resources/css/public.css" media="all" />
</head>
<body >
<div class="layuimini-container">
<!--搜索条件开始-->
	 <div class="layuimini-main">
		<fieldset class="layui-elem-field layuimini-search">
			<legend>搜索信息</legend>
			<div style="margin: 10px 10px 10px 10px">
				<form class="layui-form layui-form-pane" lay-filter="searchFrm" id="searchFrm" name="searchFrm" method="post">
					<div class="layui-form-item">
						<div class="layui-inline">
						    <label class="layui-form-label">客户</label>
						    <div class="layui-input-inline">
								<select name="customerid" class="customerSel">
									<option value ="">请选择客户</option>
								</select>
						    </div>
						</div>
						<div class="layui-inline">
						    <label class="layui-form-label">商品名称</label>
						    <div class="layui-input-inline">
								<select name="goodsid" class="goodsSel">
									<option value ="">请选择商品</option>
								</select>
						    </div>
						</div>
						<div class="layui-inline">
						    <label class="layui-form-label">开始时间</label>
						    <div class="layui-input-inline">
						        <input type="text" name="startTime" id="startTime" readonly="readonly" autocomplete="off" class="layui-input">
						    </div>
						</div>
						<div class="layui-inline">
						    <label class="layui-form-label">结束时间</label>
						    <div class="layui-input-inline">
						        <input type="text" name="endTime" id="endTime" readonly="readonly" autocomplete="off" class="layui-input">
						    </div>
						</div>
					</div>
					<div class="layui-form-item" style="text-align: center">
						<div class="layui-inline">
							<button type="button" class="layui-btn layui-btn-normal  layui-icon layui-icon-search" lay-filter="doSearch" lay-submit="">查询</button>
							<button type="reset" class="layui-btn layui-icon layui-icon-refresh">重置</button>
						</div>
					</div>
		</form>
			</div>
		</fieldset>
		<!--搜索条件结束-->
	
<!--搜索条件结束-->

<!--数据表格开始-->
	
		<table class="layui-hide" id="SalesTable" lay-filter="SalesTable"></table>
		<div id="SalesToolBar" style="display: none;">
			<button type="button" lay-event="addSales" class="layui-btn layui-btn-sm "><span class="layui-icon layui-icon-add-circle"></span>添加</button>
			<button type="button" lay-event="batchDelete" class="layui-btn layui-btn-sm layui-btn-danger"><span class="layui-icon layui-icon-delete"></span>批量删除</button>
		</div>
		<div id="SalesRowBar" style="display: none">
			<button type="button" lay-event="delete" class="layui-btn layui-btn-sm layui-btn-danger"><span class="layui-icon layui-icon-delete"></span>删除</button>
			<button type="button" lay-event="update" class="layui-btn layui-btn-sm "><span class="layui-icon layui-icon-set-sm"></span>更新</button>
			<button type="button" lay-event="back" class="layui-btn layui-btn-sm layui-btn-warm"><span class="layui-icon layui-icon-play"></span>退货</button>
		</div>
	</div>
	
</div>
<!--数据表格结束-->

<!--添加和修改的弹出层开始-->
<div style="display: none;padding: 20px" id="saveOrUpdateDiv" >
    <form class="layui-form layui-form-pane"  lay-filter="dataFrm" id="dataFrm">
		<div class="layui-form-item">
			<div class="layui-inline">
				<label class="layui-form-label">客户</label>
				<div class="layui-input-inline">
						<input type="text" name="id" class="layui-hide"/>
						<select name="customerid" class="customerSel" id="customerSel">
							<option value ="">请选择客户</option>
						</select>
				</div>
			</div>
			
			<div class="layui-inline">
				<label class="layui-form-label">商品名称</label>
				<div class="layui-input-inline">
					   <select name="goodsid" class="goodsSel" id="goodsSel">
					   	<option value ="">请选择商品</option>
					   </select>
				</div>
			</div>
		</div>
		
        <div class="layui-form-item">
			  <div class="layui-inline">
				<label class="layui-form-label">销售数量</label>
				<div class="layui-input-inline">
					<input type="text" name="number" lay-verify="required|number" autocomplete="off" class="layui-input">
				</div>
			  </div>
			  <div class="layui-inline">
					<label class="layui-form-label">销售价格</label>
					<div class="layui-input-inline">
						<input type="text" name="saleprice" lay-verify="required|number" autocomplete="off" class="layui-input">
					</div>
			  </div>
         </div>
		 
         <div class="layui-form-item">
			<label class="layui-form-label">支付类型</label>
			<div class="layui-input-block">
				<input type="radio" name="paytype" value="支付宝" title="支付宝" checked="">
				<input type="radio" name="paytype" value="微信" title="微信">
				<input type="radio" name="paytype" value="银联" title="银联">
			</div>
          </div>
		<div class="layui-form-item">
				<label class="layui-form-label">备注</label>
				<div class="layui-input-block">
				  <textarea placeholder="请输入备注" name="remark" class="layui-textarea"></textarea>
				</div>
		 </div>

        <div class="layui-form-item" style="text-align: center;">
            <div class="layui-input-block">
                <button type="button" class="layui-btn layui-btn-normal layui-btn-sm layui-icon layui-icon-release" lay-filter="doSubmit" lay-submit="">提交</button>
                <button type="reset" id="dataFrmResetBtn" class="layui-btn layui-btn-warm layui-btn-sm layui-icon layui-icon-refresh" >重置</button>
            </div>
        </div>
    </form>
</div>

<!--添加和修改的弹出层结束-->

<!-- 退货的弹出层  开始 -->
<div id="backGoodsDiv" style="display: none;padding: 5px">
    <form action="" method="post" class="layui-form layui-form-pane" id="dataBackFrm" lay-filter="dataBackFrm">
        <div class="layui-form-item">
            <label class="layui-form-label">退货数量</label>
            <div class="layui-input-block">
				<input type="text" class="layui-hide" name="goodsid" id="back_goodsid" /> 
				<input type="text" class="layui-hide" name="salesid" id="salesid" />
                <input type="text" class="layui-input"  name="number" lay-verify="required|checkgoodsnumber|number" placeholder="请输入退货数量">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">退货备注</label>
            <div class="layui-input-block">
                <textarea  name="remark" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="text-align: center;">
                <button type="button" class="layui-btn" lay-submit="" lay-filter="doBackSubmit" id="doBackSubmit" ><span class="layui-icon layui-icon-add-1"></span>提交</button>
                <button type="reset" class="layui-btn layui-btn-warm"><span class="layui-icon layui-icon-refresh-1"></span>重置</button>
            </div>
        </div>
    </form>
</div>
<!-- 退货的弹出层  结束 -->

<script src="../../../../resources/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="../../../../resources/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="../../../../resources/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="../../../../resources/lib/jq-module/jquery.particleground.min.js" charset="utf-8"></script>
<script src="../../../../resources/common/jquery.cookie.min.js" charset="utf-8"></script>
<script src="../../../../resources/common/common.js" charset="utf-8"></script>

<script>
    layui.use(['jquery','layer','form','laydate','table'],function () {
        var form=layui.form;
        var $=layui.jquery;
        var layer=layui.layer;
        var laydate=layui.laydate;
        var table=layui.table;

        //渲染时间选择器
        laydate.render({
            elem:'#startTime',
            type:'datetime'
        })

        laydate.render({
            elem:'#endTime',
            type:'datetime'
        })
		
		//加载查询条件里面的客户的下拉列表
		$.get(api+"customer/getAllAvailableCustomer",function(res){
			var search_customerid=$(".customerSel");
			var html="<option value =''>请选择客户</option>";
			var data=res.data;
			$.each(data,function(index,item){
				html+="<option value ="+item.id+">"+item.customername+"</option>"
			});
			search_customerid.html(html);
			form.render("select");
		});
		
		//加载查询条件里面的商品的下拉列表
		$.get(api+"goods/getAllAvailableGoods",function(res){
			var search_goodsid=$(".goodsSel");
			var html="<option value =''>请选择商品</option>";
			var data=res.data;
			$.each(data,function(index,item){
				html+="<option value ="+item.id+">"+item.goodsname+"["+item.size+"]"+"</option>"
			});
			search_goodsid.html(html);
			form.render("select");
		});
		

        var tableIndex = table.render({
            elem: '#SalesTable'
            ,url: api+'sales/loadAllSales'
            ,toolbar: '#SalesToolBar' //开启头部工具栏，并为其绑定左侧模板
            ,title: '销售表'
            ,page: true
            ,cols: [ [
                {field: 'id', title: 'ID', align: "center",width:80},
                {field: 'customername',  title: '客户', align: "center",width:150},
                {field: 'goodsname',  title: '商品名称', align: "center",width:150},
                {field: 'size',  title: '规格', align: "center",width:150},
                {field: 'salestime',  title: '销售时间', align: "center",width:180},
                {field: 'operateperson',  title: '操作人', align: "center",width:120},
                {field: 'number',  title: '销售数量', align: "center",width:120},
                {field: 'saleprice',  title: '销售价格', align: "center",width:120},
                {field: 'paytype',  title: '支付类型', align: "center",width:120},
                {field: 'remark',  title: '备注', align: "center",width:150},
                {fixed: 'right', title:'操作', toolbar: '#SalesRowBar',align:'center',width:300}
            ] ]
            ,done:function(data,curr,count){
                //不是第一页如果返回的数据为0那么就返回上一页
                if(data.data.length==0&&curr!=1){
                    tableIndex.reload({
                        page:{
                            curr:curr-1
                        }
                    });
                }
            }
        });

        //模糊查询
        form.on('submit(doSearch)',function(data){
            var params = $("#searchFrm").serialize();
            tableIndex.reload({
                url: api+"sales/loadAllSales?" + params,
                page:{
                    curr:1
                }
            })
            return false;
        });

        //头工具栏事件
        table.on('toolbar(SalesTable)', function(obj){
            switch(obj.event){
                case 'batchDelete':
                    BatchDelete();
                    break;
                case 'addSales':
                    openAddSales();
                    break;
            };
        });


        //监听行工具条的事件
        table.on("tool(SalesTable)",function(obj){
            var data = obj.data; //获得当前行数据
            switch(obj.event){
                case 'delete':
                    DeleteSales(data);
                    break;
                case 'update':
                    UpdateSales(data);
                    break;
                case 'back':
                    openBack(data);
                    break;
            };
        });
		

        var url;
        var mainIndex;

        //添加添加页面
        function openAddSales() {
            mainIndex = layer.open({
                type:1,
                title:'添加销售 ',
                content:$("#saveOrUpdateDiv"),
                area:['800px','550px'],
                success:function(index){
					
					$("#customerSel").attr("disabled", false);
					$("#goodsSel").attr("disabled", false);
					
                    $("#dataFrm")[0].reset();
                    url= api+"sales/addSales";
                }
            });
        }

        //更新页面
       function UpdateSales(data){
           mainIndex = layer.open({
               type:1,
               title:'添加销售 ',
               content:$("#saveOrUpdateDiv"),
               area:['800px','550px'],
               success:function(index){
				   
                   url= api+"sales/updateSales";
				   
				  form.val("dataFrm", data);
				   //下拉框不被修改
				  $("#customerSel").attr("disabled", true);
				  $("#goodsSel").attr("disabled", true);
				  form.render();
               }
           });
        }

        //保存
        form.on("submit(doSubmit)",function(obj){
            //序列化表单数据
            var params=$("#dataFrm").serialize();
            $.post(url,params,function(obj){
                layer.msg(obj.msg);
                //关闭弹出层
                layer.close(mainIndex);
                //刷新数据 表格
                if (obj.code ==200){
                    tableIndex.reload();
                }
            })
        });

		//删除
        function  DeleteSales(data) {
            layer.confirm('你确定要删除这条数据吗?', {icon: 3, title:'提示'}, function(index){
                $.post( api+"sales/deleteSales",{id:data.id},function(res){
                    if(res.code==200){
                        tableIndex.reload();
                    }
                    layer.msg(res.msg);
                })
                layer.close(index);
            });
        }
        //批量删除
        function BatchDelete() {
            //得到选中行
            var checkStatus = table.checkStatus('SalesTable');
            var dataLength = checkStatus.data.length;
            console.log(dataLength);
            if (dataLength>0){
                layer.confirm('你确定要删除这些数据吗?', {icon: 3, title:'提示'}, function(index){
                    var data = checkStatus.data; //获取选中行的数据
                    var ids = '';
                    $.each(data,function (index,item) {
                        if (index == 0){
                            ids += 'ids='+ item.id;
                        }else{
                            ids += '&ids='+ item.id;
                        }
                    })
                    $.post( api+"sales/batchDeleteSales",ids,function (res) {
                        if(res.code==200){
                            tableIndex.reload();
                        }
                        layer.msg(res.msg);
                    })
                    layer.close(index);
                })
            }else{
                layer.msg("请选中行");
            }
        }

       
		//退货
		function openBack(data){
			mainIndex = layer.open({
			    type:1,
			    title:"{"+data.goodsname+"}的退货",
			    content:$("#backGoodsDiv"),
			    area:['800px','550px'],
			    success:function(index){
				
					form.val("dataBackFrm",data);
					form.val("dataBackFrm",{number:'',salesid:data.id,remark:''});
			    }
			});
		}
		//提交数据
		form.on("submit(doBackSubmit)",function(data){
			var params = $("#dataBackFrm").serialize();
			$.post(api+"salesback/addSalesback",params,function(res){
				layer.msg(res.msg);
				layer.close(mainIndex);
			})
		})
		
		//验证退货数量
		form.verify({
			checkgoodsnumber:function(value,item){
				var str;
				var goodsid = $("#back_goodsid").val();
				$.ajax({
					url:api+"goods/getGoodsByGoodsId?goodsid="+goodsid,
					type:'get',
					async:false,
					success:function(res){
						if(res.data.number<value){
							str="退货数量不能大于当前的商品库存"
						}
					}
				})
				return str;
			}
		})

    })
</script>
</body>
</html>